package com.noble.meether.notify.util;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.noble.glamour.module.logic.application.App;
import com.noble.glamour.module.logic.socket.CoreService;
import com.noble.meether.notify.LockScreenMgr;

/**
 * 锁屏状态监听receiver
 */
public class LockScreenReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            String action = intent.getAction();
            if (action.equals(Intent.ACTION_SCREEN_OFF)) {
                LockScreenMgr.getInstance().reenableKeyguard();
                App.isKeyguard = App.AppKeyguard.KG_SCREEN_OFF;
                // CoreService持有电源锁
                if (null != CoreService.wakeLock && !(CoreService.wakeLock.isHeld())) {
                    CoreService.wakeLock.acquire();
                }

            }else if (action.equals(Intent.ACTION_SCREEN_ON)) {
                App.isKeyguard = App.AppKeyguard.KG_SCREEN_ON;
                // 释放电源锁
                if (null != CoreService.wakeLock && CoreService.wakeLock.isHeld()) {
                    CoreService.wakeLock.release();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
